import { request } from '../utils/request';

// code-knowledge服务，真实调用 /unit/pageQuery
export async function fetchUnitList(params: { pageNum: number; pageSize: number;[key: string]: any }) {
    return request({ url: '/unit/config/pageQuery', method: 'post', data: params });
}

// code-sa-analyser服务示例
export async function fetchSaAnalyse(params: any) {
    return request({ url: '/sa-analyser/xxx', method: 'get', params });
}

// 新增单元
export async function createUnit(data: { appId: string; unitId: string; artifactoryPath?: string; unitType: string; ownTeam: string; ownCenter?: string; gitRepoUrl: string;[key: string]: any }) {
    return request({ url: '/unit/config/create', method: 'post', data });
}

// 更新单元
export async function updateUnit(data: { appId: string; unitId: string; artifactoryPath?: string; unitType: string; ownTeam: string; ownCenter?: string; gitRepoUrl: string;[key: string]: any }) {
    return request({ url: '/unit/config/update', method: 'put', data });
}

// 查询单元详情
export async function getUnit(unitId: string) {
    return request({ url: '/unit/config/get', method: 'get', params: { unitId } });
}

// 删除单元
export async function deleteUnit(unitId: string) {
    return request({ url: '/unit/config/delete', method: 'delete', params: { unitId } });
}

// 手动触发扫描
export async function manualTriggerScan(unitId: string) {
    return request({ url: '/unit/scantask/manualTrigger/', method: 'post', params: { unitId } });
}

// 手动触发指定类型的扫描任务
export async function manualTriggerScanByType(unitId: string, taskType: string) {
    return request({ 
        url: '/unit/scantask/manualTrigger/', 
        method: 'post', 
        params: { unitId, tasks: taskType } 
    });
}

// 获取应用下的业务单元列表（用于添加应用时显示）
export async function getBusinessUnitsForApplication(appId?: string) {
    const url = appId ? `/application/${appId}/business-units` : '/application/business-units';
    return request({ url, method: 'get' });
}

// 获取应用列表
export async function getApplicationList() {
    return request({ url: '/application/list', method: 'get' });
}

// 根据应用ID获取单元列表
export async function getUnitsByAppId(appId: string) {
    return request({ url: `/application/${appId}/units`, method: 'get' });
}

// 根据单元ID获取单元详情（用于自动填充）
export async function getUnitDetails(unitId: string) {
    return request({ url: `/application/units/${unitId}/details`, method: 'get' });
}

// 获取圈复杂度最高的TOP N方法
export async function getTopComplexityMethods(unitId: string, limit: number = 10) {
    return request({ url: '/unit/config/topComplexityMethods', method: 'get', params: { unitId, limit } });
}

// 批量扫描相关API

// 触发应用级批量扫描
export async function triggerBatchScan(appId: string, data: {
    taskName?: string;
    unitIds: string[];
    taskTypes?: string[];
    creator?: string;
}) {
    return request({ url: `/application/${appId}/batch-scan`, method: 'post', data });
}

// 获取批量扫描进度
export async function getBatchScanProgress(batchTaskId: number) {
    return request({ url: `/application/batch-scan/${batchTaskId}`, method: 'get' });
}

// 获取应用批量扫描历史
export async function getBatchScanHistory(appId: string) {
    return request({ url: `/application/${appId}/batch-scan/history`, method: 'get' });
}

// 取消批量扫描任务
export async function cancelBatchScan(batchTaskId: number) {
    return request({ url: `/application/batch-scan/${batchTaskId}/cancel`, method: 'post' });
} 